/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eventdisplay;

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.*;

/**
 *
 * @author kurthewett
 */
public class aboutJWindow {

    private JWindow window = new JWindow();
    public ImageIcon splashIcon = new ImageIcon(getClass().getResource("/eventdisplay/splash.png"));
    public Image splashImage = splashIcon.getImage();

    public aboutJWindow(String verStr) {
        String htmlLabel0 = "<html><font color=\"#ffd900\" size=\"2\"><p align=\"right\">Version " + verStr + "</p></html>";
        String htmlLabel1 = "<html><font color=\"#ffd900\" size=\"2\"><p align=\"right\">Developed by</p></html>";
        String htmlLabel4 = "<html><font color=\"#ffd900\" size=\"2\"><p align=\"right\">UMD</p></html>";

        int width = splashImage.getWidth(null);
        int height = splashImage.getHeight(null);

        window.setSize(width, height);
        window.setLocation(500, 200);
        window.setAlwaysOnTop(true);
        window.setBackground(new Color(0, 0, 0, 0));
        window.setLayout(null);
        
        Insets insets = window.getInsets();

        JLabel splashLabel = new JLabel(splashIcon);
        splashLabel.setSize(new Dimension(width, height));
        splashLabel.setBackground(new Color(0, 0, 0, 0));
        splashLabel.setBorder(null);

        JLabel splashText1 = new JLabel();
        splashText1.setText(htmlLabel1);
        Dimension splashTextSize1 = splashText1.getPreferredSize();
        splashText1.setBounds(388 + insets.left, 310 + insets.top, splashTextSize1.width, splashTextSize1.height);
        
        SwingLink splashText2 = new SwingLink("Kuravi Hewawasam", "http://www.d.umn.edu/~hewaw001/");
        Dimension splashTextSize2 = splashText2.getPreferredSize();
        splashText2.setBounds(458 + insets.left, 310 + insets.top, splashTextSize2.width, splashTextSize2.height);

        SwingLink splashText3 = new SwingLink("Visualization and Digital Imaging Lab", "http://www.d.umn.edu/vdil/");
        Dimension splashTextSize3 = splashText3.getPreferredSize();
        splashText3.setBounds(342 + insets.left, 325 + insets.top, splashTextSize3.width, splashTextSize3.height);

        JLabel splashText4 = new JLabel();
        splashText4.setText(htmlLabel4);
        Dimension splashTextSize4 = splashText4.getPreferredSize();
        splashText4.setBounds(530 + insets.left, 325 + insets.top, splashTextSize4.width, splashTextSize4.height);

        SwingLinkButton splashText5 = new SwingLinkButton("Close","Close");
        splashText5.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                window.dispose();
            }
        });
        Dimension splashTextSize5 = splashText5.getPreferredSize();
        splashText5.setBounds(525 + insets.left, 340 + insets.top, splashTextSize5.width, splashTextSize5.height);

        
        window.add(splashText1);
        window.add(splashText2);
        window.add(splashText3);
        window.add(splashText4);
        window.add(splashText5);
        window.add(splashLabel);
        

    }

    public void openWindow() {
        window.setVisible(true);
    }

    public void closeWindow() {
        window.setVisible(false);
        window.dispose();
    }
}


class SwingLinkButton extends JLabel {
    private String text;
    
    public SwingLinkButton(String text,String tip) {
        super();
        setup(text,tip);
    }
    
    public void setup(String t,String tip) {
        text = t;
        setText(text);
        setToolTipText(tip);
        addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                setText(text, false);
            }

            public void mouseExited(MouseEvent e) {
                setText(text, true);
            }
        });
    }
    
    @Override
    public void setText(String text) {
        setText(text, true);
    }
    
    public void setText(String text, boolean ul) {
        String link = ul ? "<u>" + text + "</u>" : text;
        super.setText("<html><p align=\"right\"><font size=\"2\"><span style=\"color: #ffd900;\">"
                + link + "</span></p></html>");
        this.text = text;
    }

}







class SwingLink extends JLabel {

    //private static final long serialVersionUID = 8273875024682878518L;
    private String text;
    private URI uri;

    public SwingLink(String text, URI uri) {
        super();
        setup(text, uri);
    }

    public SwingLink(String text, String uri) {
        super();
        URI oURI;
        try {
            oURI = new URI(uri);
        } catch (URISyntaxException e) {
            // converts to runtime exception for ease of use
            // if you cannot be sure at compile time that your
            // uri is valid, construct your uri manually and
            // use the other constructor.
            throw new RuntimeException(e);
        }
        setup(text, oURI);
    }

    public void setup(String t, URI u) {
        text = t;
        uri = u;
        setText(text);
        setToolTipText(uri.toString());
        addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                open(uri);
            }

            public void mouseEntered(MouseEvent e) {
                setText(text, false);
            }

            public void mouseExited(MouseEvent e) {
                setText(text, true);
            }
        });
    }

    @Override
    public void setText(String text) {
        setText(text, true);
    }

    public void setText(String text, boolean ul) {
        String link = ul ? "<u>" + text + "</u>" : text;
        super.setText("<html><p align=\"right\"><font size=\"2\"><span style=\"color: #ffd900;\">"
                + link + "</span></p></html>");
        this.text = text;
    }

    public String getRawText() {
        return text;
    }

    private static void open(URI uri) {
        if (Desktop.isDesktopSupported()) {
            Desktop desktop = Desktop.getDesktop();
            try {
                desktop.browse(uri);
            } catch (IOException e) {
                JOptionPane.showMessageDialog(null,
                        "Failed to launch the link, "
                        + "your computer is likely misconfigured.",
                        "Cannot Launch Link", JOptionPane.WARNING_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null,
                    "Java is not able to launch links on your computer.",
                    "Cannot Launch Link", JOptionPane.WARNING_MESSAGE);
        }
    }
}